﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.Font;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldType;
	import flash.text.TextFieldAutoSize;
	
	/**
	  Basic ShapeObject Database is meant to solve problems of size, color
	  and information regarding each ShapeObject-type. A basic class has only
	  functions needed by both Player and Editor, it has to bee as little as
	  possible.
	*/
	public class BasicShapeObjectDatabase{
		
		static private const sArrow:uint = 0;
		static private const sCellL:uint = 1;
		static private const sCellM:uint = 2;
		static private const sCellS:uint = 3;
		static private const sCircleL:uint = 4;
		static private const sCircleM:uint = 5;
		static private const sCircleS:uint = 6;
		static private const sGroupL:uint = 7;
		static private const sGroupM:uint = 8;
		static private const sGroupS:uint = 9;
		static private const sProcessL:uint = 10;
		static private const sProcessM:uint = 11;
		static private const sProcessS:uint = 12;
		static private const sText:uint = 13;
		static private const sConsoleL:uint = 14;
		static private const sConsoleM:uint = 15;
		static private const sConsoleS:uint = 16;
		static private const sThreadL:uint = 17;
		static private const sThreadM:uint = 18;
		static private const sThreadS:uint = 19;
		static private const sThreadBall:uint = 20;
		static private const sThreadRoll:uint = 21;
		static private const sCustom:uint = 22;
		
		/**
		  ClassList is accessed though static constants in order to increase
		  readability. It contains all shape's of player.
		*/
		static private const classes:Array = [PlayerShapeArrow,
							   PlayerShapeCellL, PlayerShapeCellM, PlayerShapeCellS,
							   PlayerShapeCircleL, PlayerShapeCircleM, PlayerShapeCircleS,
							   PlayerShapeGroupL, PlayerShapeGroupM, PlayerShapeGroupS,
							   PlayerShapeProcessL, PlayerShapeProcessM, PlayerShapeProcessS,
							   PlayerShapeText,
							   PlayerShapeConsoleL, PlayerShapeConsoleM, PlayerShapeConsoleS,
							   PlayerShapeThreadL, PlayerShapeThreadM, PlayerShapeThreadS,
							   PlayerShapeThreadBall,
							   PlayerShapeThreadRoll,
							   PlayerShapeCustom
							   ];
		
		/**
		  Query if shape of the player is able to be editable, as editable we
		  understand that it is possible to paint on it
		  @param shape ShapeObject to test if it can be edited
		  @param myClasses Array with the list of classes to use to check
		     the database. If none specified, this class' classList is used.
			 (Null by default).
		  @return Boolean with true if it is editable, false otherwise
		*/
		static public function isEditable(shape:BasicShapeObject, myClasses:Array = null):Boolean{
			var ret:Boolean = false;
			
			if(myClasses == null) myClasses = classes;
			
			if(shape is myClasses[sCustom]){
				ret = true;
			}
			return ret;
		}
		
		/**
		  Query if shape of the player is able to have text
		  @param shape ShapeObject to test if it has text
		  @param myClasses Array with the list of classes to use to check
		     the database. If none specified, this class' classList is used.
			 (Null by default).
		  @return Boolean with true if it can have, false otherwise
		*/
		static public function hasText(shape:BasicShapeObject, myClasses:Array = null):Boolean{
			var ret:Boolean = true;
			
			if(myClasses == null) myClasses = classes;
			
			if(shape is myClasses[sArrow] ||
			   shape is myClasses[sCircleL] ||
			   shape is myClasses[sCircleM] ||
			   shape is myClasses[sCircleS]
			   ){
				ret = false;
			}
			return ret;
		}
		
		/**
		  Returns a new TextField associated to a ShapeObject. This
		  TextField has the font and format specific to the ShapeObject
		  type. Note this function does NOT check if the ShapeObject can
		  have a TextField or not, this should by checked before calling
		  this function.
		  @param shape ShapeObject of which we want to get the new TextField
		  @param myClasses Array with the list of classes to use to check
		     the database. If none specified, this class' classList is used.
			 (Null by default).
		  @return TextField of the specified ShapeObject
		*/
		static public function getTextField(shape:BasicShapeObject, myClasses:Array = null):TextField{
			var tfield:TextField 	= new TextField();
			var tformat:TextFormat 	= new TextFormat();
			
			if(myClasses == null) myClasses = classes;
			
			if(shape is myClasses[sConsoleL] ||
				shape is myClasses[sConsoleM] ||
				shape is myClasses[sConsoleS]){
				tformat.font = (new Font_Sans()).fontName;
				tformat.align 	= "left";
			}else{
				tformat.font = (new Font_SegoePrint()).fontName;
				tformat.align = "center";
			}
			
			tformat.size			= 20; // Default size for big ShapeObjects
			tformat.color			= 0x333333; // Default color for ShapeObjects
			
			// Set font size
			if(shape is myClasses[sCellM] ||
			   shape is myClasses[sGroupM] ||
			   shape is myClasses[sProcessM] ||
			   shape is myClasses[sThreadL]
			   ){
				tformat.size = 18;
			}else if(shape is myClasses[sText] ||
					shape is myClasses[sConsoleL] ||
					shape is myClasses[sConsoleM] ||
					shape is myClasses[sConsoleS] ||
					shape is myClasses[sThreadM]){
				tformat.size = 16;
			}else if(shape is myClasses[sThreadS]){
				tformat.size = 14;
			}else if(shape is myClasses[sCellS] ||
			   shape is myClasses[sGroupS] ||
			   shape is myClasses[sProcessS]
			   ){
				tformat.size = 12;
			}
			
			// Set text color
			if(shape is myClasses[sGroupL] ||
			   shape is myClasses[sGroupM] ||
			   shape is myClasses[sGroupS]
			   ){
				tformat.color = 0x006633;
			}
			
			tfield.type				= TextFieldType.DYNAMIC;
			tfield.multiline		= true;
			tfield.wordWrap			= true;
			tfield.selectable		= false;
			tfield.embedFonts		= true;
			tfield.restrict			= getRestrictedCharacters();
			tfield.antiAliasType 	= AntiAliasType.ADVANCED;
			tfield.defaultTextFormat = tformat; // Assign the text format
			tfield.text				= "ID"; // Default text
			
			// Set the text by default of the ShapeObject
			if(shape is myClasses[sGroupL] ||
			   shape is myClasses[sGroupM] ||
			   shape is myClasses[sGroupS]){
				tfield.text = "Group";
			}else if(shape is myClasses[sText]){
				tfield.text = "Write your text here";
			}else if(shape is myClasses[sConsoleL] ||
					shape is myClasses[sConsoleM] ||
					shape is myClasses[sConsoleS]){
				tfield.text = "Write your code here";
			}
			
			// Set text size
			if(shape is myClasses[sText] ||
				shape is myClasses[sConsoleL] ||
				shape is myClasses[sConsoleM] ||
				shape is myClasses[sConsoleS]){
				tfield.height = shape.getHeight();
			}else{
				tfield.autoSize = TextFieldAutoSize.CENTER;
			}
			tfield.width = shape.getWidth();
			
			// Set text position
			tfield.x = shape.x - shape.getWidth()/2;
			tfield.y = shape.y - tfield.height/2;
			
			return tfield;
		}
		
		/**
		  Returns the characters that ShapeObjects are able to show
		*/
		static public function getRestrictedCharacters():String{
			return "0-9a-zA-Z \".,:;¡!¿?ñÑçÇ[]()+*/_'\\-{}#·áéíóúÁÉÍÓÚ~`´\\^@=|&";
		}
	}
	
}